package com.example.client_anna.module.ui;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.viewpager2.widget.ViewPager2;

import com.example.client_anna.R;
import com.example.client_anna.base.BaseActivity;
import com.example.client_anna.util.adapter.Page2FragmentAdapter;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cy
 * @email 272884043@qq.com
 * @date 2021/10/19
 */
public class ViewCarrier extends BaseActivity {

    private static final String TAG = "ViewCarrier";

    private BottomNavigationView bottomNavigationView;
    private ViewPager2 viewPager2;

    private static Boolean init_Interface = false;


    //在Toolbar加载菜单栏
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }
    //菜单点击触发事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.settings:
                Toast.makeText(this,"You Clicked Settings",Toast.LENGTH_SHORT).
                        show();
                break;
            default:
        }
        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_carrier);

        Toolbar toolbar = (Toolbar)findViewById(R.id.Toobar);
        setSupportActionBar(toolbar);

        //隐藏标题栏
        //ActionBar actionBar = getSupportActionBar();
        //if(actionBar!=null){
        //    actionBar.hide();
        //}

        //半透明状态栏
        Window window = this.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //隐藏状态栏
        //View decorView = window.getDecorView();
        //decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

        //获取底部导航栏实例
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);

        //viewPager2加载Fragment
        viewPager2 = findViewById(R.id.view_Carrier2);

        //接收MainActivity传递过来的用户名
        //intenttransfer intenttransfer = (intenttransfer) getIntent().getSerializableExtra("user_info");

        //访问存储在本地的文件，通过键值对的方式读取想要的信息出来
        SharedPreferences preferences =getSharedPreferences("UserInfo", Context.MODE_PRIVATE);
        //读取key为user_role中的值，如果没有数据就赋值0给key
        //String.valueOf强类型转换
        String userrole = String.valueOf(preferences.getInt("user_role",0));

        int status = -1;
        if(true==init_Interface){
            Intent intent = getIntent();
            Bundle bundle = intent.getExtras();
            status = bundle.getInt("module_status");
        }


        init_Interface = true;

        //根据登录成功后的role值对用户的类型进行判断，1：普通用户 2：商户 3：配送员
        //加载不同的Fragment

        switch (userrole){
            case "1":
                //普通用户
                List<Fragment> fragmentList = new ArrayList<>();
                fragmentList.add(new HomeFragment());
                fragmentList.add(new ExploreFragment());
                fragmentList.add(new OrderFragment());
                fragmentList.add(new UserFragment());
                viewPager2.setAdapter(Page2FragmentAdapter.start(this,fragmentList));
                //预先缓存
                viewPager2.setOffscreenPageLimit(fragmentList.size());
                //禁用滑动
                viewPager2.setUserInputEnabled(false);
                break;

            case "2":
                //商家
                break;

            case "3":
                //配送员
                break;
        }

        switch (status){
            case 0:
                //首页
                break;
            case 1:
                //分享
                break;
            case 2:
                //订单
                viewPager2.setCurrentItem(status,false);
                bottomNavigationView.getMenu().getItem(status).setChecked(true);
                break;
            case 3:
                //用户
                break;
            default:
                break;
        }

        //底部导航栏的点击跳转事件
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull  MenuItem item) {
                int menuId = item.getItemId();
                //点击底部导航栏取消viewPager2的滑动效果,参数2：false
                switch (menuId){
                    //View_Home在Menu菜单项中的值为0,当点击它时跳转到相应的ViewPage页面
                    case R.id.View_Home:
                        viewPager2.setCurrentItem(0,false);
                        break;
                    case R.id.View_Explore:
                        viewPager2.setCurrentItem(1,false);
                        break;
                    case R.id.View_Order:
                        viewPager2.setCurrentItem(2,false);
                        break;
                    case R.id.View_User:
                        viewPager2.setCurrentItem(3,false);
                        break;
                }
                return false;
            }
        });

        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                bottomNavigationView.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });

    }

}